﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using wms.Common;

namespace wms
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Dictionary<string, TabItem> dic = new Dictionary<string, TabItem>();
        public static MainWindow Win { get; set; }

        public MainWindow()
        {
            Auth.Check();
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString() );
            InitializeComponent();
            Win = this;
            this.tbx_operator.Text = Auth.Name;

            //MessageBox.Show(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).ProductVersion);
            //System.Reflection.Assembly.GetEntryAssembly();
        }

        public void CreateTabCard(string name, string uri)
        {
            /*
            Grid grid = new Grid();
            if (am.Source.EndsWith("xaml"))
            {
                Frame frame = new Frame();
                //new System.Uri(am.Source, UriKind.Relative);
                frame.Source = new Uri(am.Source, UriKind.Relative);
                grid.Children.Add(frame);
            }
            else
            {
                WebBrowser webBrowser = new WebBrowser();
                webBrowser.Source = new Uri(Config.GetServerURL() + am.Source);
                grid.Children.Add(webBrowser);
            }
            */

            if (dic.ContainsKey(name))
            {
                dic[name].IsSelected = true;
                return;
            }

            Frame frame = new Frame();
            frame.Source = new Uri(uri, UriKind.Relative);
            TabItem tabItem = new TabItem();
            TextBlock text = new TextBlock(new Run(name));
            tabItem.Header = text;
            tabItem.Tag = name;
            tabItem.Content = frame;
            tabItem.IsSelected = true;
            text.MouseLeftButtonDown += DoubleClickHeader;
            //tabItem.BorderThickness = new Thickness(0);
            //tabItem.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff0000"));
            //frame.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
            //tabItem.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff0000")); 
            this.tabControl.Items.Add(tabItem);
            dic.Add(name, tabItem);
        }

        private void DoubleClickHeader(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                TabItem item = (sender as TextBlock).Parent as TabItem;
                dic.Remove((string)item.Tag);
                this.tabControl.Items.Remove(item);
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_navigation_click(object sender, RoutedEventArgs e)
        {
            MenuItem button = sender as MenuItem;
            this.CreateTabCard(button.Header.ToString(), button.Tag as string);
        }


        private void button_test(object sender, RoutedEventArgs e)
        {
            Pages.Type win1 = new Pages.Type();
            win1.Show();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.tbx_version.Text = System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).ProductVersion;
            //Thread thread = new Thread(UpdateConnectionState);
            //thread.Start();


        }
        //private void UpdateConnectionState()
        //{
        //    while (true)
        //    {
        //        Dictionary<string, string> dic = DBHelper.State();
        //        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
        //        {
        //            if (dic["result"] == "success")
        //            {
        //                this.tbx_connection.Text = "正常";
        //                this.tbx_connection.Foreground = Brushes.Black;
        //                this.tbx_connection.ToolTip = null;
        //            }
        //            else
        //            {
        //                this.tbx_connection.Text = "异常";
        //                this.tbx_connection.ToolTip = dic["message"];
        //                this.tbx_connection.Foreground = Brushes.Red;
        //            }

        //        });
        //        Thread.Sleep(1000);
        //    }
        //}

        private void Window_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
